﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Foliatura;

namespace FoliaturaDigital
{

    
    public partial class Frm_Expediente : Form
    {
        private Foliatura.Controller controlador;
        private Panel buttonPanel = new Panel();
        
        public Frm_Expediente()
        {
            controlador = Foliatura.Controller.Instance();
            InitializeComponent();
        }

        private void Frm_Expediente_Load(object sender, EventArgs e)
        {
            DataBind();         
        }
        public void DataBind()
        {
            controlador.expediente = new BussinesLogic.Expediente();
            controlador.expedientes = BussinesLogic.Expediente.getExpedientes();
            ArrayList arr = controlador.expedientes;
            dgExpedientes.DataSource = arr;

            if (dgExpedientes.Rows.Count > 0)
            {
                SetupLayout(true);
                SetupDataGridView();
            }
            else
            {
                SetupLayout(false);
            }
        }
        

        private void btnPortada_Click(object sender, EventArgs e)
        {
            //this.controlador.expediente = this.controlador.expedientes.Select( .[this.dgExpedientes.CurrentCellAddress.Y].i;
            if (dgExpedientes.SelectedRows.Count > 0)
            {
                FoliaturaDigital.Frm_Portada port = new FoliaturaDigital.Frm_Portada();
                port.ShowDialog();
            }
            else
            {
                MessageBox.Show("No ha seleccionado ningun expediente");
            }
        }

        private void btnDocumentos_Click(object sender, EventArgs e)
        {
            if (dgExpedientes.SelectedRows.Count > 0)
            {
                FoliaturaDigital.Frm_Documentos docs = new FoliaturaDigital.Frm_Documentos();
                docs.ShowDialog();
            }
            else
            {
                MessageBox.Show("No ha seleccionado ningun expediente");
            }
            
        }

        private void dgExpedientes_SelectionChanged(object sender, EventArgs e)
        {
            if (this.dgExpedientes.SelectedRows.Count > 0)
                controlador.expediente = (BussinesLogic.Expediente)controlador.expedientes[dgExpedientes.CurrentRow.Index];
            
        }

        private void dgExpedientes_CellFormatting(object sender, System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
        {
            if (this.dgExpedientes.Columns[e.ColumnIndex].Name == "Release Date")
            {
                if (e != null)
                {
                    if (e.Value != null)
                    {
                        try
                        {
                            e.Value = DateTime.Parse(e.Value.ToString())
                                .ToLongDateString();
                            e.FormattingApplied = true;
                        }
                        catch (FormatException)
                        {
                            Console.WriteLine("{0} is not a valid date.", e.Value.ToString());
                        }
                    }
                }
            }
        }

        private void SetupLayout(bool empty)
        {
            if (empty)
            {
                this.Size = new Size(600, 500);

                btn_crear.Location = new Point(10, 8);
                this.btnActualizar.Location = new Point(100, 8);
                btnDocumentos.Location = new Point(190, 8);
                btnPortada.Location = new Point(280, 8);
                btnCerrar.Location = new Point(370, 8);

                /*deleteRowButton.Text = "Delete Row";
                deleteRowButton.Location = new Point(100, 10);
                deleteRowButton.Click += new EventHandler(deleteRowButton_Click);*/

                buttonPanel.Controls.Add(btn_crear);
                buttonPanel.Controls.Add(btnDocumentos);
                buttonPanel.Controls.Add(btnPortada);
                buttonPanel.Controls.Add(btnActualizar);
                buttonPanel.Controls.Add(btnCerrar);

                buttonPanel.Height = 40;
                buttonPanel.Dock = DockStyle.Bottom;

                this.Controls.Add(this.buttonPanel);
                if (dgExpedientes.Rows.Count > 0)
                    dgExpedientes.Rows[0].Selected = true;
                this.Size = new Size(640, 480);
            }
            else
            {
                this.Size = new Size(200, 100);
            }
            buttonPanel.Visible = empty;
            dgExpedientes.Visible = empty;
            btnActualizar.Visible = empty;
            btnDocumentos.Visible = empty;
            btnPortada.Visible = empty;
            
        }

        private void SetupDataGridView()
        {
            this.Controls.Add(dgExpedientes);

            //dgExpedientes.ColumnCount = 5;

            dgExpedientes.ColumnHeadersDefaultCellStyle.BackColor = Color.Navy;
            dgExpedientes.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
            dgExpedientes.ColumnHeadersDefaultCellStyle.Font =
                new Font(dgExpedientes.Font, FontStyle.Bold);

            dgExpedientes.Name = "dgExpedientes";
            dgExpedientes.Location = new Point(8, 8);
            dgExpedientes.Size = new Size(500, 250);
            dgExpedientes.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
            dgExpedientes.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
            dgExpedientes.CellBorderStyle = DataGridViewCellBorderStyle.Single;
            dgExpedientes.GridColor = Color.Black;
            dgExpedientes.RowHeadersVisible = false;

            
            dgExpedientes.Columns[0].Visible = false;
            dgExpedientes.Columns[1].Visible = true;
            dgExpedientes.Columns[1].Width = 200;
            dgExpedientes.Columns[2].Visible = false;
            dgExpedientes.Columns[3].Visible = false;
            dgExpedientes.Columns[4].Visible = false;
            dgExpedientes.Columns[5].Visible = false;
            dgExpedientes.Columns[6].Visible = true;
            dgExpedientes.Columns[6].Width = 400;
            dgExpedientes.Columns[6].DefaultCellStyle.Font =
                new Font(dgExpedientes.DefaultCellStyle.Font, FontStyle.Italic);

            dgExpedientes.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgExpedientes.MultiSelect = false;
            dgExpedientes.Dock = DockStyle.Fill;
            dgExpedientes.DefaultCellStyle.SelectionForeColor = System.Drawing.Color.White;
            dgExpedientes.DefaultCellStyle.SelectionBackColor = System.Drawing.Color.Black;
            dgExpedientes.DefaultCellStyle.ForeColor = System.Drawing.Color.Black;
            dgExpedientes.DefaultCellStyle.BackColor = System.Drawing.Color.White;

            dgExpedientes.CellFormatting += new DataGridViewCellFormattingEventHandler(dgExpedientes_CellFormatting);
        }

        private void btn_crear_Click(object sender, EventArgs e)
        {
            this.controlador.expediente = new BussinesLogic.Expediente();
            FoliaturaDigital.frmCrearExpediente port = new FoliaturaDigital.frmCrearExpediente(this);
            port.ShowDialog();
        }

        private void btnActualizar_Click(object sender, EventArgs e)
        {
            Foliatura.DocumentBrowser port = new Foliatura.DocumentBrowser();
            port.ShowDialog();
        }

        private void btnCerrar_Click(object sender, EventArgs e)
        {
            controlador.ApplicationClose();
        }

        private void dgExpedientes_DoubleClick(object sender, EventArgs e)
        {
            if (dgExpedientes.Rows.Count > 0)
            {
                controlador.expediente = ((BussinesLogic.Expediente)this.dgExpedientes.SelectedRows[0].DataBoundItem);
                //controlador.expediente = ((BussinesLogic.Expediente)this.dgExpedientes.Rows[e.RowIndex].DataBoundItem);
                FoliaturaDigital.frmCrearExpediente port = new FoliaturaDigital.frmCrearExpediente(this);
                port.ShowDialog();
            }
            
        }

        

        
    }
}
